﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.DirectoryServices.AccountManagement;
using AccountManagement.Entities;
using AccountManagement.Utility;
using System.DirectoryServices;

namespace AccountManagement.Managers
{
    public enum ObjectType
    {
        User,
        Group
    }

    public class GroupManager
    {
        private static List<Group> GetValidGroups(PrincipalSearchResult<Principal> groups)
        {
            List<Group> result = new List<Group>();
            // iterate over all groups
            foreach (Principal p in groups)
            {
                // make sure to add only group principals
                if (p is GroupPrincipal)
                {
                    result.Add(Helper.ConvertToGroup((GroupPrincipal)p));
                }
            }

            return result;
        }
        
        public static List<Group> GetMemberOf(string objName, ObjectType objtype)
        {

            List<Group> result = new List<Group>();
            // establish domain context
            PrincipalContext yourDomain = new PrincipalContext(ContextType.Domain, Constants.DOMAIN_NAME);

            // find your memberof
            if (ObjectType.User == objtype)
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(yourDomain, objName);
                return (user != null) ? GetValidGroups(user.GetGroups()) : result; 
            }
            else if (ObjectType.Group == objtype)
            {
                GroupPrincipal group = GroupPrincipal.FindByIdentity(yourDomain, objName);
                return (group != null) ? GetValidGroups(group.GetGroups()) : result; 
            }

            return result;
        }

        public static List<Group> GetGroups()
        {
            List<Group> lstADGroups = new List<Group>();
            try
            {
                string DomainPath = String.Format("LDAP://{0}", Constants.DOMAIN_DN);
                DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
                DirectorySearcher search = new DirectorySearcher(searchRoot);
                search.Filter = "(&(objectClass=group))";
                search.PropertiesToLoad.Add(Constants.PROP_NAME);               
                search.PropertiesToLoad.Add(Constants.PROP_SAM_ACCOUNT_NAME);
                search.PropertiesToLoad.Add(Constants.PROP_DESCRIPTION);

                SearchResult result;
                SearchResultCollection resultCol = search.FindAll();
                if (resultCol != null)
                {
                    for (int counter = 0; counter < resultCol.Count; counter++)
                    {
                        string UserNameEmailString = string.Empty;
                        result = resultCol[counter];

                        Group group = new Group();
                        group.Name = Helper.GetStringValue(result, Constants.PROP_NAME);
                        group.SamAccountName = Helper.GetStringValue(result, Constants.PROP_SAM_ACCOUNT_NAME);
                        group.Description = Helper.GetStringValue(result, Constants.PROP_DESCRIPTION);
                        group.GroupType = 0;

                        lstADGroups.Add(group);
                    }
                }

                return lstADGroups;
            }
            catch (Exception ex)
            {

            }

            return lstADGroups;
        }
    }
}